﻿package com.tvein.sanguo.swc.action.command
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.Slider;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class Command95 extends MovieClip
	{
		public static const NAME:String = "Command-95";
		
		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var targetID:int;
		private var roleEnergy:int;
		private var roleEnergyAdd:int;
		private var roleMoney:int;
		private var unitTime:int;
		private var unitPrice:int;
		
		private var timeSlider:Slider;
		
		public function Command95()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtName.border = true;
			txtName.borderColor = 0x333333;
			txtMoney.border = true;
			txtMoney.borderColor = 0x333333;
			
			timeSlider = new Slider();
			timeSlider.x = 250;
			timeSlider.y = 310;
			timeSlider.width = 240;
			timeSlider.height = 3;
			timeSlider.maximum = 100;
			timeSlider.minimum = 0;
			timeSlider.snapInterval = 10;
			timeSlider.tickInterval = 10;
			timeSlider.enabled = false;
			this.addChild(timeSlider);
			
			btnStart.visible = false;
			btnTimeMinus.visible = false;
			btnTimePlus.visible = false;
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnStart.addEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setTargetId":
					targetID = params.TargetId;
					break;
				case "setData":
					setData(params);
					reRoad();
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			for (var i:int = 0; i < params.length; i ++)
			{
				for (var k:String in params[i]) 
				{
					switch (k)
					{
						case "EnergyNum":
							roleEnergy = params[i][k];
							break;
						case "EnergyAdd":
							roleEnergyAdd = params[i][k];
							break;
						case "RoleMoney":
							roleMoney = params[i][k];
							break;
						case "UnitTime":
							unitTime = params[i][k];
							break;
						case "UnitPrice":
							unitPrice = params[i][k];
							break;
						case "Name":
							txtName.text = params[i][k];
							break;
					}
				}
			}
		}
		
		private function reRoad():void
		{
			if (timeSlider.value == 0)
			{
				txtTime.text = "";
				btnTimeMinus.visible = false;
				btnStart.visible = false;
			}
			else
			{
				txtTime.text = "（" + timeSlider.value * unitTime / 10 + " 分钟）";
				btnTimeMinus.visible = true;
				btnStart.visible = true;
			}
			
			if ((timeSlider.value + 10) * unitTime / 10 <= (roleEnergy + roleEnergyAdd) && (timeSlider.value + 10) * unitPrice / 10 <= roleMoney)
			{
				btnTimePlus.visible = true;
			}
			else
			{
				btnTimePlus.visible = false;
			}
			
			txtMoney.text = String(timeSlider.value * unitPrice / 10);
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "Command-95"]));
					break;
				case "btnStart":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["CommandCall(95,CommandUse," + targetID + ",actTime=" + timeSlider.value / 10 * unitTime + ");"]]));
					break;
				case "btnTimeMinus":
					timeSlider.value -= 10;
					reRoad();
					break;
				case "btnTimePlus":
					timeSlider.value += 10;
					reRoad();
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnStart.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}